package com.leo;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @ClassName AnimationTimerDemo
 * @Description 动画定时器
 * @Author wangss
 * @date 2020.10.21 21:13
 * @Version 1.0
 */
public class AnimationTimerDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox(50);
        ToggleGroup tg = new ToggleGroup();

        ToggleButton tb1 = new ToggleButton("播放");
        tb1.setToggleGroup(tg);

        ToggleButton tb2 = new ToggleButton("暂停");
        tb2.setToggleGroup(tg);

        ToggleButton tb3 = new ToggleButton("停止");
        tb3.setToggleGroup(tg);

        ToggleButton tb4 = new ToggleButton("跳转");
        tb4.setToggleGroup(tg);

        AnimationTimer animationTimer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                System.out.println(now);
            }
        };

        hBox.getChildren().addAll(tb1, tb2, tb3, tb4);
        AnchorPane an = new AnchorPane();
        an.getChildren().addAll(hBox);
        AnchorPane.setTopAnchor(hBox, 100d);
        AnchorPane.setLeftAnchor(hBox, 100d);
        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ToggleButton");
        primaryStage.setWidth(1200d);
        primaryStage.setHeight(800d);
        primaryStage.setAlwaysOnTop(true);
        primaryStage.show();

        addAction(animationTimer, tb1, tb2, tb3, tb4);
    }

    private void addAction(AnimationTimer timeline, ToggleButton tb1, ToggleButton tb2, ToggleButton tb3, ToggleButton tb4) {
        tb1.setOnAction(event -> {
            timeline.start();
        });

        tb2.setOnAction(event -> {
        });

        tb3.setOnAction(event -> {
            timeline.stop();
        });

        tb4.setOnAction(event -> {

        });
    }
}
